package com.tvserials.notification.parser;

import com.tvserials.notification.dto.Episode;
import com.tvserials.notification.dto.Serial;
import com.tvserials.notification.exceptions.NetworkConnectionException;
import com.tvserials.notification.vo.SourceType;
import com.tvserials.notification.vo.TvEpisodeFilterVO;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Created by Alex on 15.04.2014.
 */
public class NovaFilmHtmlPageParser implements PageParser {
    @Override
    public List<Episode> parsePage(TvEpisodeFilterVO filterVO) throws NetworkConnectionException {
        try {
            Connection connection = Jsoup
                    .connect("http://novafilm.tv/auth/login")
                    .header("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
                    .header("Accept-Encoding","gzip,deflate,sdch")
                    .header("Content-Type","multipart/form-data")
                    .cookie("nfb","a%3A3%3A%7Bi%3A0%3Bs%3A7%3A%221243986%22%3Bi%3A1%3Bs%3A40%3A%22a33e6cd16f4b0912d01366e5d4aa8617713c0311%22%3Bi%3A2%3Bi%3A1429523216%3B%7D")
                    .data("username", "dokich")
                    .data("password", "Login1!")
                    .data("login", "Хочу войти!")
                    .data("returnto", "http://novafilm.tv/")
                    .method(Connection.Method.POST);

            Connection.Response res = connection.execute();
            System.out.println("code: " + res.statusCode());
            System.out.println("res: " + res.parse().toString());
            Document documents = connection.url("http://novafilm.tv/do/browse/?page=1").get();
//            System.out.println(documents.toString());

            //Keep logged in
            Map<String, String> cookies = res.cookies();
            Connection connect = Jsoup.connect("http://novafilm.tv/do/browse/?page=1");
            connect.cookies(cookies);
//            connect.cookie("uid", "1451668");
//            connect.cookie("pass", "e9069f323d82764cd8988913ea524be8");
            connect.timeout(10 * 1000);
            Document document = connect.get();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public List<Serial> parseSerialsList() throws NetworkConnectionException {
        return null;
    }

    @Override
    public SourceType getSourceType() {
        return null;
    }
}
